<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Tools\ReleaseUserFromGag;
use App\Admin\Extensions\Tools\TopUp;
use App\Admin\Extensions\Tools\UpdateUserLoginStatus;
use App\Models\User;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;

class UsersController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.users'))
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.users'))
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.users'))
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.users'))
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);
        $grid->model()->where('role', User::CUSTOMER);
        $grid->id('Id');
        $grid->user_nu(trans('admin.user_nu'));
        $grid->username(trans('admin.username'));
        $grid->email(trans('admin.email'));
        $grid->nick_name(trans('admin.nick_name'));
        $grid->avatar(trans('admin.avatar'))->lightbox(['width' => 50, 'height' => 50]);
        $grid->wallet()->balance(trans('admin.balance'));
        $grid->gag_duration(trans('admin.gag_duration'))->display(function ($value) {
            if ($value) {
                return $value . trans('admin.minutes');
            }
        });
        $grid->created_at(trans('admin.created_at'));
        $grid->filter(function ($filter) {
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('user_nu', trans('admin.user_nu'));
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->like('username', trans('admin.username'));
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->like('email', trans('admin.email'));
            });
        });
        $grid->disableCreateButton();
        $grid->actions(function ($action) {
            $action->disableEdit();
            $action->prepend('<a href="/reset_password/' . $this->row->id . '" title="' . trans('admin.reset_password') . '"><i class="fa fa-eye-slash fa-fw"></i></a>');
            $action->prepend(new ReleaseUserFromGag($action->getKey(), $action->row->gag_status));
            $action->prepend(new UpdateUserLoginStatus($action->getKey(), $action->row->login_status));
            $action->prepend(new TopUp($action->getKey()));
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->id('Id');
        $show->user_no(trans('admin.user_nu'));
        $show->username(trans('admin.username'));
        $show->email(trans('admin.email'));
        $show->nick_name(trans('admin.nick_name'));
        $show->avatar(trans('admin.avatar'))->image();
//        $show->role(trans('admin.role'));
        $show->login_status(trans('admin.login_status'))->using(User::$LOGIN_STATUS_MAP);
        $show->gag_status(trans('admin.gag_status'))->using(User::$GAG_STATUS_MAP);
        $show->created_at(trans('admin.created_at'));
        $show->updated_at(trans('admin.updated_at'));
        $show->wallet(trans('admin.wallet'), function ($data) {
            $data->setResource('/sellers');
            $data->balance(trans('admin.balance'));
            $data->alipay_account(trans('admin.alipay_account'));
            $data->true_name(trans('admin.true_name'));
            $data->bank_account(trans('admin.bank_account'));
            $data->bank(trans('admin.bank'));

            $data->panel()
                ->tools(function ($tools) {
                    $tools->disableEdit();
                    $tools->disableList();
                    $tools->disableDelete();
                });
        });
        $show->panel()
            ->tools(function ($tools) {
                $tools->disableEdit();
//                $tools->disableList();
//                $tools->disableDelete();
            });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);
        $form->display('user_nu', trans('admin.user_nu'));
        $form->display('username', trans('admin.username'))->rules('required|unique:users,username')->setLabelClass(['required']);
        $form->display('email', trans('admin.email'))->rules('required|unique:users,email')->setLabelClass(['required']);

//        $form->password('password', trans('admin.password'))->rules('required')->setLabelClass(['required']);
//        $form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required|same:password')->setLabelClass(['required']);

        $form->display('nick_name', trans('admin.nick_name'));
        $form->image('avatar', trans('admin.avatar'));
        $form->hidden('role', trans('admin.role'))->options(User::$ROLE_MAP)->default(User::CUSTOMER);
//        $form->radio('login_status', trans('admin.login_status'))->options(User::$LOGIN_STATUS_MAP)->default(User::NORMAL);
//        $form->ignore(['password_confirmation']);
        $form->saving(function ($form) {
//            $form->model()->password = bcrypt($form->input('password'));
        });

        return $form;
    }

    /**
     * 设置密码
     *
     * @param $user_id
     * @param Content $content
     * @param Request $request
     * @return Content|\Symfony\Component\HttpFoundation\Response
     */
    public function resetPassword($user_id, Content $content, Request $request)
    {
        if ($request->isMethod('get')) {
            return $content
                ->header(trans('admin.menu_titles.users'))
                ->description(trans('admin.reset_password'))
                ->body($this->passwordForm($user_id)->edit($user_id));
        } else {
            return $this->passwordForm($user_id)->update($user_id);
        }
    }

    /**
     * 设置密码表单
     *
     * @param $user_id
     * @return Form
     */
    protected function passwordForm($user_id)
    {
        $form = new Form(new User);

        $form->display('user_nu', trans('admin.user_nu'));
        $form->password('password', trans('admin.password'))->rules('required')->setLabelClass(['required']);
        $form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required|same:password')->setLabelClass(['required']);
        $form->setAction(route('admin.user.password', ['user' => $user_id]));
        $form->ignore(['password', 'password_confirmation']);
        $form->saving(function ($form) {
            $form->model()->password = bcrypt($form->input('password'));
        });
        $form->disableEditingCheck();
        $form->disableCreatingCheck();
        $form->disableViewCheck();
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
            $tools->disableList();
            $tools->add('<a href="' . route('users.index') . '" class="btn btn-sm btn-default" title="列表"><i class="fa fa-list"></i><span class="hidden-xs">&nbsp;列表</span></a>');
        });

        return $form;
    }

}
